package br.inf.solutions.apresentacoes.ssp.control.bulletin;

import br.com.vexillum.util.HibernateUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.PublicationController;
import br.inf.solutions.apresentacoes.ssp.control.SearchPersonController;
import br.inf.solutions.apresentacoes.ssp.model.Publication;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.Bulletin;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinAwaitItens;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinPublishedItem;
import br.inf.solutions.apresentacoes.ssp.model.interfaces.IBulletinItem;
import br.inf.solutions.apresentacoes.ssp.model.interfaces.IExistsPerson;

public class BaseBulletinController<E extends IExistsPerson> extends SearchPersonController<E> {
	
	private BulletinAwaitItensController awaitController;

	public BaseBulletinController(Class<E> classEntity) {
		super(classEntity);
	}
	
	protected Publication getNotPublishedPublication(){
		PublicationController controller = SpringFactory.getController("publicationController", PublicationController.class, null);
		return controller.getDefaultPuplication(3l);
	}
	
	protected Publication getPublishedPublication(){
		PublicationController controller = SpringFactory.getController("publicationController", PublicationController.class, null);
		return controller.getPublicatedPuplication();
	}
	
	protected Publication getAwaitingPublication(){
		PublicationController controller = SpringFactory.getController("publicationController", PublicationController.class, null);
		return controller.getDefaultPuplication();
	}
	
	protected Return savePublishedItem(BulletinAwaitItens item, Bulletin bulletin){
		Return ret = new Return(true);
		
		item.getItem().setPublication(getPublishedPublication());
		item.getItem().setBulletin(bulletin);
		
		BulletinPublishedItem i = new BulletinPublishedItem();
		i.setBulletin(bulletin);
		i.setItem(item.getItem());
		i.setTopic(item.getTopic());
		
		BulletinPublishedItensController controller = SpringFactory.getController("bulletinPublishedItensController", BulletinPublishedItensController.class, null);
		controller.setEntity(i);
		
		ret.concat(controller.save());
		if(ret.isValid()){
			ret.concat(deleteAwaitItem(item));
		}
		
 		return ret;
	}
	
	protected Return deleteAwaitItem(BulletinAwaitItens item){
		BulletinAwaitItensController controller = getBulletinAwaitItensController();
		controller.setEntity(item);
		
		return controller.delete();
	}
	
	protected Return realocateItem(BulletinAwaitItens item){
		BulletinAwaitItensController controller = getBulletinAwaitItensController();
		controller.setEntity(item);
		
		return controller.realocate();
	}
	
	protected Return deleteBulletinItem(IBulletinItem item){
		String sql = "DELETE " + HibernateUtils.materializeProxy(item).getClass().getSimpleName() + " WHERE id = " + item.getId();
		data.put("sql", sql);
		return executeByHQL();
	}
	
	protected BulletinAwaitItensController getBulletinAwaitItensController(){
		if(awaitController == null){
			awaitController = SpringFactory.getController("bulletinAwaitItensController", BulletinAwaitItensController.class, null);
		}
		return awaitController;
	}
	
}
